//MPI c++ wrapper
//$Id: easy_mpi.cpp 667 2011-02-26 18:59:32Z Oleg.Bulychov $

#include "stdafx.h"

#include "src/heo/include/easy_mpi.h"

//__________________________________________________________________________________
#define STR__(x) STRINGIZE_A(x)
#define LOC__ __FILE__ "(" STR__(__LINE__) "): "
//__________________________________________________________________________________
//__________________________________________________________________________________
namespace mpi
{
//__________________________________________________________________________________
implementation::TTypeMap implementation::s_map;
implementation::TBuffer implementation::s_buffer;
implementation::TIsendBuffer implementation::s_isend_list;
//__________________________________________________________________________________
isend_buffer& implementation::alloc_Isend(void* pt, size_t cb)
{
    isend_buffer buff = {};
    buff.cb_ = cb;
    if (cb)
    {
        if (cb <= sizeof(buff.dummy_))
        {
            memcpy(&buff.dummy_, pt, cb);
        }
        else
        {
            buff.pv_ = malloc(cb);
            memcpy(buff.pv_, pt, cb);
        }
    }
    s_isend_list.push_back(buff);
    return s_isend_list.back();
}
//.............................................................................
void implementation::test_Isend()
{
    int count = (int)(s_isend_list.size());
    for (int i = count - 1; i >= 0; --i)
    {
        isend_buffer& b = s_isend_list[i];
        int f = 0;
        MPI_Status st = {};
        Test(&b.request_, &f, &st);
        if (f)
        {
            if (b.cb_ > sizeof(b.dummy_))
            {
                //std::cout << "free mem " << b.cb_ << std::endl;
                free(b.pv_);
            }
            s_isend_list.erase(s_isend_list.begin() + i);
        }
    }
}
//.............................................................................
}
//__________________________________________________________________________________
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)

#if defined(MPICH_NAME)

#if (2 == MPI_VERSION)

#pragma comment(lib, "mpi")

#elif (1 == MPI_VERSION)

#if defined(_DEBUG)
#pragma comment(lib, "mpichd")
#else
#pragma comment(lib, "mpich")
#endif

#endif//VERSION

#elif defined(MS_MPI)

//:TODO approve
#pragma comment(lib, "msmpi")

#else

#pragma message(LOC__ "add *mpi* lib here")

#endif

#endif//_MSC_VER
//__________________________________________________________________________________
//__________________________________________________________________________________

//EOF!
